package com.sql.common.util;

/**
 * Created by Shisan on 2017/11/24.
 */
public class NameUtil {

    /**
     * 将_改为驼峰命名法
     * @param str
     * @return
     */
    public static String toJavaNamed(String str){
        String arr[] = str.split("_");
        StringBuilder sb = new StringBuilder(arr[0].toLowerCase());
        for(int i=1;i<arr.length;i++){
            sb.append(toFirstUp(arr[i]));
        }
        return sb.toString();
    }

    /**
     * 驼峰命名法转为_
     * @param str
     * @return
     */
    public static String toDatabaseNamed(String str){
        char c[] = str.toCharArray();
        StringBuilder sb = new StringBuilder(String.valueOf(c[0]).toUpperCase());
        String temp;
        for(int i=1;i<c.length;i++){
            temp= String.valueOf(c[i]);
            if(temp.matches("[A-Z]")){
                sb.append("_");
                sb.append(temp);
            }else{
                sb.append(temp.toUpperCase());
            }
        }
        return sb.toString();
    }

    public static String toFirstUp(String str){
        return str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase();
    }

    public static void main(String args[]){
        System.out.println(toDatabaseNamed("userName"));
        System.out.println(toJavaNamed("USER_NAME"));
    }

}
